package com.sz.common.core.service.sms;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.utils.JsonUtils;
import com.sz.common.core.system.dto.SmsBodyDto;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by xutao on 17/5/27.
 */
@Service("SmsService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class SmsServiceImpl extends AbstractService implements SmsService {

    @Value("${sms.accessid}")
    private String smsAccessId;
    @Value("${sms.accesskey}")
    private String smsAccessKey;
    @Value("${sms.signname}")
    private String smsSignName;

    @Override
    public void sendSms(String mobilePhone, String templateCode,
                        SmsBodyDto smsBodyDto) {
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        final String product = "Dysmsapi";//短信API产品名称
        final String domain = "dysmsapi.aliyuncs.com";//短信API产品域名

        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", smsAccessId, smsAccessKey);
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        } catch (ClientException e1) {
            e1.printStackTrace();
        }
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        try{
            request.setSignName(smsSignName);
            request.setTemplateCode(templateCode);

            Map<String,String> map = new HashedMap();

            for (int i=0; i<smsBodyDto.getSmsTemplateKey().size(); i++) {

                map.put(smsBodyDto.getSmsTemplateKey().get(i),smsBodyDto.getSmsTemplateValue().get(i));
            }

            String paramString = JsonUtils.toJSONString(map);

            request.setTemplateParam(paramString);
            request.setPhoneNumbers(mobilePhone);
            SendSmsResponse httpResponse = client.getAcsResponse(request);

            String aaa = httpResponse.getCode();

        }catch(ServerException e) {
            e.printStackTrace();
        }catch(ClientException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String send(String mobilePhone, String templateCode,
                       SmsBodyDto smsBodyDto) throws ClientException {
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

        final String product = "Dysmsapi";//短信API产品名称
        final String domain = "dysmsapi.aliyuncs.com";//短信API产品域名

        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", smsAccessId, smsAccessKey);
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setSignName(smsSignName);
        request.setTemplateCode(templateCode);

        Map<String,String> map = new HashedMap();

        for (int i=0; i<smsBodyDto.getSmsTemplateKey().size(); i++) {

            map.put(smsBodyDto.getSmsTemplateKey().get(i),smsBodyDto.getSmsTemplateValue().get(i));
        }

        String paramString = JsonUtils.toJSONString(map);

        request.setTemplateParam(paramString);
        request.setPhoneNumbers(mobilePhone);
        SendSmsResponse httpResponse = client.getAcsResponse(request);
        return  httpResponse.getCode();
    }
}
